/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */
package ChatSystem;

import ChateoController.*;

import ChateoFacadeGUI.FacadeGUI;

import ChateoFacadeNI.FacadeNI;

import ChateoGUI.ChatGUI;

import java.net.SocketException;

import java.net.UnknownHostException;

import java.util.Scanner;

/**
 *
 *
 *
 * @author adrian
 *
 * @author olga
 *
 */
public class ChatSystem {

    public static void main(String[] args) throws Exception {

        boolean GUI = true;

        //With GUI
        if (GUI) {

            Scanner sc = new Scanner(System.in);

            System.out.println("Veuillez maintenant entrer le port du serveur");

            int port = Integer.parseInt(sc.nextLine());

            Controller controller = new Controller(port);

            FacadeGUI facadegui = new FacadeGUI(controller);

            ChatGUI gui = new ChatGUI(facadegui);

            FacadeNI facadeni = new FacadeNI(controller, port);

            controller.setFacadeni(facadeni);

            controller.setFacadegui(facadegui);

            facadegui.setGui(gui);

        } //Without GUI
        else {

            try {

                Scanner sc = new Scanner(System.in);

                System.out.println("Entrez votre login");

                String str = sc.nextLine();

                System.out.println("Bienvenue " + str);

                System.out.println("Veuillez maintenant entrer le port du serveur");

                int port = Integer.parseInt(sc.nextLine());

                Controller controller = new Controller(str, port);

                controller.connect();

                while (true) {

                    System.out.println("Enter 1 to send a message, enter something else to disconnect");

                    String choice = sc.nextLine();

                    if (choice.equals("1")) {

                        System.out.println("This is the list of connected users " + controller.getUserList());

                        System.out.println("Enter the login of your friend");

                        String Dest = sc.nextLine();

                        System.out.println("Enter your message");

                        String Mensaje = sc.nextLine();

                        System.out.println("Enter 1 to send a private message, enter something else to send a broadcast message");

                        String publico = sc.nextLine();

                        if (publico.equals("1")) {

                            controller.mensajesBasicos(Mensaje, Dest, true);

                        } else {

                            controller.mensajesBasicos(Mensaje, Dest, false);

                        }

                    } else {

                        controller.disconnect();

                    }

                }

                /*Thread.sleep(15000);

                 if (controller.DebugMode()){

                 System.out.println("Début de la phase de deconnexion");

                 }

   

                

                

                 controller.disconnect();*/
            } catch (NumberFormatException nfe) {

                System.out.println("NFE");

            } catch (NullPointerException npe) {

                System.out.println("NPE");

            } catch (SocketException se) {

                System.out.println("SE");

            } catch (UnknownHostException uhe) {

                System.out.println("UHE");

            } catch (Exception uhe) {

                System.out.println("ICH BIN SCHNAPPI");

            }

        }

    }

}
